package sui.utils.csv;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;

/**
 * <p>
 * Description: 读取CSV文件,生成SQL
 * <p>
 * @author XQF SUI
 * <p>
 * Create Date: 2015年7月9日 下午7:40:12
 * <p>
 * @version 1.0
 */
public class CsvToSQL {
	
	public static final String GROUPIDS_FILE_PATH = "C:\\Users\\Administrator\\Desktop\\GROUPIDS.csv";
	public static final String GROUP_TREE_FILE_PATH = "C:\\Users\\Administrator\\Desktop\\GROUP_tree.csv";
	
	private static Set<String> groupIdsSet = new HashSet<String>();
	
	
	
	@SuppressWarnings("resource")
	public static void main(String[] args) throws IOException {
		
		initGroupIdsSet();
		
		BufferedReader reader = new BufferedReader(
				new InputStreamReader(new FileInputStream(GROUP_TREE_FILE_PATH), "GBK"));
		
		reader.readLine();// 第一行信息，为标题信息，不用,如果需要，注释掉
		
		String line = null;
		int i = 0;
		
		while ((line = reader.readLine()) != null) {
			String item[] = line.split(",");// CSV格式文件为逗号分隔符文件，这里根据逗号切分
			for (String str : item) {
				if (groupIdsSet.contains(str)) {
					i++;
				}
			}
		}
		
		System.out.println(i);
	}
	
	@SuppressWarnings("resource")
	public static boolean initGroupIdsSet() throws IOException {
		
		BufferedReader reader = new BufferedReader(
				new InputStreamReader(new FileInputStream(GROUPIDS_FILE_PATH), "GBK"));
		
		reader.readLine();// 第一行信息，为标题信息，不用,如果需要，注释掉
		
		String line = null;
		
		groupIdsSet = new HashSet<String>();	
		
		while ((line = reader.readLine()) != null) {
			String item[] = line.split(",");// CSV格式文件为逗号分隔符文件，这里根据逗号切分
			
			for (String str : item) {
				groupIdsSet.add(str.replace("\"", ""));
			}
		}
		
		return true;
	}
}
